package order

import (
	"gitee.com/micro-plat/qtask"
	"gitee.com/micro-plat/qtask/internal/modules/const/field"
	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/types"
)

type RequestHandler struct {
}

func NewRequestHandler() (u *RequestHandler) {
	return &RequestHandler{}
}

//Handle .
func (u *RequestHandler) Handle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Info("----------------订单创建---------------------")
	queueName := ctx.Request().GetString("queuename")
	queueName = types.GetString(queueName, "ORDER-PAY")
	db := hydra.C.DB().GetRegularDB(qtask.GetDBName())

	_, callbacks, err := qtask.Create(db, field.TaskID, "2222", map[string]interface{}{
		"order_id": "3333331333333",
	}, 30, queueName, qtask.WithDeadline(600), qtask.WithDeleteDeadline(1000), qtask.WithMaxCount(10), qtask.WithExtNO("3333333"), qtask.WithMaxCount(0))
	if err != nil {
		return err
	}

	if err = callbacks(ctx); err != nil {
		return err
	}
	return "success"
}

// DelayHandle 延迟函数
func (u *RequestHandler) DelayHandle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Info("----------------订单创建---------------------")

	_, err := qtask.Delay(ctx, "订单支付任务－延迟", "111", map[string]interface{}{
		field.OrderNo: "87698990232",
	}, 300, 300, "ORDER-PAY", qtask.WithDeadline(86400), qtask.WithDeleteDeadline(1000))
	if err != nil {
		return err
	}

	return "success"
}

// CloseHandle 订单关闭函数
func (u *RequestHandler) CloseHandle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Info("----------------订单关闭函数---------------------")

	err := qtask.Close(ctx.Request().GetInt64(field.TaskID))
	if err != nil {
		return err
	}

	return "success"
}

// UpdateHandle 延迟函数
func (u *RequestHandler) UpdateHandle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Info("----------------订单更新---------------------")

	err := qtask.Update(ctx.Request().GetInt64(field.TaskID), qtask.WithUpdateDeadline(86400), qtask.WithUpdateDeleteDeadline(1000))
	if err != nil {
		return err
	}

	return "success"
}
